package com.ffdms.mobileoffice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class NoticeActivity extends Activity {
	private TextView tv_title;
	private Button btn_return;
	private Button btn_popup;
	private PopupWindow popupWindow;
	private ListView lv_group;
	private View view;
	private List<String> groups;
	private TextView tv_notice_title2;
	private TextView tv_notice_time2;
	private TextView tv_notice_content2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_notice);

		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_title.setText("系统公告");

		btn_return = (Button) findViewById(R.id.btn_return);
		btn_return.setVisibility(View.VISIBLE);
		btn_return.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		btn_popup = (Button) findViewById(R.id.btn_popup);
		btn_popup.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showWindow(v);
			}
		});

		tv_notice_title2 = (TextView) findViewById(R.id.tv_plan_base_section);
		tv_notice_time2 = (TextView) findViewById(R.id.tv_notice_time2);
		tv_notice_content2 = (TextView) findViewById(R.id.tv_notice_content2);
		String title = getIntent().getStringExtra("title");
		tv_notice_title2.setText(title);
		String time = getIntent().getStringExtra("time");
		tv_notice_time2.setText(time);
		String content = getIntent().getStringExtra("content");
		tv_notice_content2.setText(content);
	}

	private void showWindow(View parent) {

		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			view = layoutInflater.inflate(R.layout.group_list, null);

			lv_group = (ListView) view.findViewById(R.id.lvGroup);
			groups = new ArrayList<String>();
			groups.add("公告列表");

			GroupAdapter groupAdapter = new GroupAdapter(this, groups);
			lv_group.setAdapter(groupAdapter);
			popupWindow = new PopupWindow(view, 200, 250);
		}

		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);

		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		int xPos = windowManager.getDefaultDisplay().getWidth() / 2
				- popupWindow.getWidth() / 2;

		Log.i("coder", "windowManager.getDefaultDisplay().getWidth()/2:"
				+ windowManager.getDefaultDisplay().getWidth() / 2);
		//
		Log.i("coder", "popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2);

		Log.i("coder", "xPos:" + xPos);

		popupWindow.showAsDropDown(parent, xPos, 0);

		lv_group.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {

				switch (position) {
				case 0:
					Intent t = new Intent(NoticeActivity.this, NoticeListActivity.class);
					startActivity(t);
					finish();
					break;
				default:
					break;
				}

				if (popupWindow != null) {
					popupWindow.dismiss();
				}
			}
		});
	}
}
